/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managerBeanAdmin;

import entity.Articles;
import entity.Category;
import entity.Complaint;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import managerBeanViews.Login;
import processor.ArticlesFacade;
import processor.CategoryFacade;
import processor.ComplaintFacade;
import processor.DepartmentFacade;

/**
 *
 * @author Phong
 */
@ManagedBean(name = "admin")
@RequestScoped
public class Index {

    @ManagedProperty(value = "#{login}")//this is EL name of your bean 
    private Login injectedBean;
    @EJB
    private ComplaintFacade complaintFacade;
    @EJB
    private ArticlesFacade articlesFacade;
    @EJB
    private DepartmentFacade departmentFacade;
    @EJB
    private CategoryFacade categoryFacade;
    private List<Category> categories;
    private List<entity.Department> departments;
    private List<Articles> articles;
    private List<Complaint> complaints;
    private Integer todayComplaints = 0;
    private Integer lastedComplaints = 0;
    private Integer solvedComplaints = 0;
    private Integer pendingComplaints = 0;
    private Integer numUnprocessComplaint = 0;

    public Index() {
    }

    public List<Complaint> getComplaints() {
        complaints = complaintFacade.getComplaintAll();
        return complaints;
    }

    public void setComplaints(List<Complaint> complaints) {
        this.complaints = complaints;
    }

    public List<Category> getCategories() {
        categories = categoryFacade.getCategoryAll();
        return categories;
    }

    public void setCategories(List<Category> categorys) {
        this.categories = categorys;
    }

    public List<entity.Department> getDepartments() {
        departments = departmentFacade.getDepartmentAll();
        return departments;
    }

    public void setDepartments(List<entity.Department> departments) {
        this.departments = departments;
    }

    public List<Articles> getArticles() {
        articles = articlesFacade.getArticlesAll();
        return articles;
    }

    public void setArticles(List<Articles> articles) {
        this.articles = articles;
    }

    /**
     * @return the todayComplaints
     */
    public Integer getTodayComplaints() {
        Date current = new Date();
        Calendar cal = Calendar.getInstance();
        Calendar cal1 = Calendar.getInstance();
        cal.setTime(current);
        for (Complaint com : getComplaints()) {
            cal1.setTime(com.getComplaintCreateDate());
            if (cal.get(Calendar.YEAR) == cal1.get(Calendar.YEAR)) {
                if (cal.get(Calendar.MONTH) == cal1.get(Calendar.MONDAY)) {
                    if (cal.get(Calendar.DAY_OF_MONTH) == cal1.get(Calendar.DAY_OF_MONTH)) {
                        todayComplaints++;
                    }
                }
            }
        }
        return todayComplaints;
    }

    /**
     * @param todayComplaints the todayComplaints to set
     */
    public void setTodayComplaints(Integer todayComplaints) {
        this.todayComplaints = todayComplaints;
    }

    /**
     * @return the lastedComplaints
     */
    public Integer getLastedComplaints() {
        lastedComplaints = getComplaints().size();
        return lastedComplaints;
    }

    /**
     * @param lastedComplaints the lastedComplaints to set
     */
    public void setLastedComplaints(Integer lastedComplaints) {
        this.lastedComplaints = lastedComplaints;
    }

    /**
     * @return the solvedComplaints
     */
    public Integer getSolvedComplaints() {
        for (Complaint com : getComplaints()) {
            if (com.getComplaintStatus() == 0) {
                solvedComplaints++;
            }
        }
        return solvedComplaints;
    }

    /**
     * @param solvedComplaints the solvedComplaints to set
     */
    public void setSolvedComplaints(Integer solvedComplaints) {
        this.solvedComplaints = solvedComplaints;
    }

    /**
     * @return the pendingComplaints
     */
    public Integer getPendingComplaints() {
        for (Complaint com : getComplaints()) {
            if (com.getComplaintStatus() == 1) {
                pendingComplaints++;
            }
        }
        return pendingComplaints;
    }

    /**
     * @param pendingComplaints the pendingComplaints to set
     */
    public void setPendingComplaints(Integer pendingComplaints) {
        this.pendingComplaints = pendingComplaints;
    }

    /**
     * @return the injectedBean
     */
    public Login getInjectedBean() {
        return injectedBean;
    }

    /**
     * @param injectedBean the injectedBean to set
     */
    public void setInjectedBean(Login injectedBean) {
        this.injectedBean = injectedBean;
    }

    /**
     * @return the numUnprocessComplaint
     */
    public Integer getNumUnprocessComplaint() {
        numUnprocessComplaint = 0;
        for (Complaint com : getComplaints()) {
            if (com.getComplaintTechnical() != null) {
                if (com.getComplaintTechnical().getMembersUser().equals(getInjectedBean().getCustomer().getMembersUser()) 
                        && com.getComplaintStatus() == 1) {
                    numUnprocessComplaint++;
                }
            }
        }
        return numUnprocessComplaint;
    }

    /**
     * @param numUnprocessComplaint the numUnprocessComplaint to set
     */
    public void setNumUnprocessComplaint(Integer numUnprocessComplaint) {
        this.numUnprocessComplaint = numUnprocessComplaint;
    }
}
